package com.rongx.reggie.config;

import com.rongx.reggie.utils.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Slf4j
@Configuration
// 关键：仅实现 WebMvcConfigurer 接口，不继承 WebMvcConfigurationSupport
public class WebMvcConfig implements WebMvcConfigurer {

    // 1. 静态资源映射（修饰符改为默认或 public）
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始进行静态资源映射...");
        registry.addResourceHandler("/backend/**")
                .addResourceLocations("classpath:/backend/"); // 对应 src/main/resources/backend/
        registry.addResourceHandler("/front/**")
                .addResourceLocations("classpath:/front/");
    }

    // 2. 扩展消息转换器
    //配置自定义的json转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器...");
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper()); // 确保该类已定义
        converters.add(0, messageConverter);
    }

    // 3. 根路径重定向（修饰符改为默认或 public）
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        log.info("配置根路径重定向...");
        registry.addViewController("/")
                .setViewName("redirect:/front/index.html");
    }
}